package org.atovio.aop.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.StringJoiner;

/**
 * @author AtoVio, Winter Yuan
 */
@Component
@Aspect
public class LogAspect {

    // 增强功能的方法
    @Before("execution(* org.atovio.aop.service..*.* (..) )")
    public void sysLog(JoinPoint jp){
        StringJoiner log = new StringJoiner("|", "{", "}");
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        log.add(dtf.format(LocalDateTime.now()));

        // 当前切入点方法的名称
        String name = jp.getSignature().getName();
        log.add(name);

        // 当前切入点方法的参数
        Object[] args = jp.getArgs();
        for (var arg : args) {
            log.add(arg == null ? "-" : arg.toString());
        }

        // 输出
        System.out.println("日志： " + log);
    }
}
